using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SourceSystemManager
{
    public enum TabStatus
    {
        stNEW_DOCUMENT = 0,         // new document without any changed
        stNEW_DOCUMENT_MODIFIED,    // new document with changed but not save
        stNEW_DOCUMENT_SAVED,       // new document, changed and saved
        stNEW_DOCUMENT_SAVED_MODIFIED,
        stOPEN_DOCUMENT,            // exists document without any changed
        stOPEN_DOCUMENT_MODIFIED,   // exists document with changed but not save
        stOPEN_DOCUMENT_SAVED,      // exists document, changed and saved
        NONE
    }

    public delegate void TabCloseEventHandler(TabView sender);

    public class TabView
    {
        #region Variables
        private Label lheader = null;

        private TextBox tInputArea = null;

        private TabStatus tabStatus;

        private Control parent = null;

        private string fileName = "";
        #endregion

        public TabView(Control parent, Label lbl, TextBox txt)
        {
            tabStatus = TabStatus.NONE;
            this.lheader = lbl;
            this.tInputArea = txt;
            this.parent = parent;

            this.fileName = lbl.Text;
        }

        #region Properties
        public TabCloseEventHandler OnTapClosed;

        public bool NameChanged { get; set; }

        public string DisplayName { get; set; }

        public string FullName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        public int ID { get; set; }

        public int Index { get; set; }

        public TabStatus Status
        {
            get { return tabStatus; }
            set { tabStatus = value; }
        }

        public Label Header
        {
            get { return lheader; }
            set { lheader = value; }
        }

        public TextBox InputArea
        {
            get { return tInputArea; }
            set { tInputArea = value; }
        }

        #endregion

        #region Methods

        public bool Close()
        {
            if (this.parent != null)
            {
                this.parent.Controls.Remove(lheader);
                this.parent.Controls.Remove(tInputArea);

                if (OnTapClosed != null)
                    OnTapClosed(this);

                return true;
            }

            return false;
        }

        public bool Add(TabStatus status)
        {
            if(this.parent != null)
            {
                this.parent.Controls.AddRange(new Control[] { lheader, tInputArea });
                tabStatus = status;

                tInputArea.BringToFront();
                tInputArea.Focus();

                lheader.BackColor = Color.Orange;
                return true;
            }
            return false;
        }

        #endregion
    }
}

